package com.xmb.wechat.view.wechat.chat;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.definterface.HintListener;
import com.xmb.wechat.definterface.SimpleSpinnerSelectedListener;
import com.xmb.wechat.util.PicChooseUtils;
import com.xmb.wechat.widget.TitleLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/14 17:11
 */
public class WechatVideoCallActivity extends BaseActivity {

    private static final int REQUEST_CODE_IMAGE_OBJECT = 102;
    private static final int REQUEST_CODE_IMAGE_MINE   = 103;

    @BindView(R2.id.title_layout)
    TitleLayout    mTitleLayout;
    @BindView(R2.id.re_receiver)
    RelativeLayout mReReceiver;
    @BindView(R2.id.re_time)
    RelativeLayout mReTime;
    @BindView(R2.id.iv_receiver)
    ImageView      mIvReceiver;
    @BindView(R2.id.tv_receiver)
    TextView       mTvReceiver;
    @BindView(R2.id.tv_to_answer)
    TextView       mTvToAnswer;
    @BindView(R2.id.tv_on_line)
    TextView       mTvOnLine;
    @BindView(R2.id.spinner_min)
    Spinner        mSpinnerMin;
    @BindView(R2.id.spinner_second)
    Spinner        mSpinnerSecond;
    @BindView(R2.id.iv_object)
    ImageView      mIvObject;
    @BindView(R2.id.re_object)
    RelativeLayout mReObject;
    @BindView(R2.id.iv_mine)
    ImageView      mIvMine;
    @BindView(R2.id.re_mine)
    RelativeLayout mReMine;
    @BindView(R2.id.btn_save)
    Button         mBtnSave;

    private boolean           isToAnswer;
    private WechatContactBean mReceiverContactBean;
    //通话中所需参数
    private int               mMin = 0;
    private int               mSec = 0;
    private String            mStrObjectImg, mStrMineImg;

    public WechatVideoCallActivity() {
        super(R.layout.activity_wechat_video_call);
    }

    @Override
    protected void initView() {
        setState(true);
        mSpinnerMin.setOnItemSelectedListener(new SimpleSpinnerSelectedListener() {
            @Override
            public void onSelected() {
                String strMin = (String) mSpinnerMin.getSelectedItem();
                mMin = Integer.parseInt(strMin.replace("分", ""));
                checkBtnState();
            }
        });
        mSpinnerSecond.setOnItemSelectedListener(new SimpleSpinnerSelectedListener() {
            @Override
            public void onSelected() {
                String strSec = (String) mSpinnerSecond.getSelectedItem();
                mSec = Integer.parseInt(strSec.replace("秒", ""));
                checkBtnState();
            }
        });
        showExemption(new HintListener() {
            @Override
            public void onConfirm() {
                //do nothing
            }

            @Override
            public void onCancel() {
                WechatVideoCallActivity.this.finish();
            }
        });
    }

    @SuppressLint("IntentReset")
    @OnClick({R2.id.re_receiver, R2.id.tv_to_answer, R2.id.tv_on_line, R2.id.re_object, R2.id.re_mine, R2.id.btn_save})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.re_receiver) {
            showRoleChooseReceiver(this, false, false, null);
        } else if (id == R.id.tv_to_answer) {
            setState(true);
        } else if (id == R.id.tv_on_line) {
            setState(false);
        } else if (id == R.id.re_object) {
            PicChooseUtils.chooseOnePicWithCut(WechatVideoCallActivity.this, 1, 2, Pic -> {
                mStrObjectImg = Pic[0];
                Picasso.get().load("file:" + mStrObjectImg).into(mIvObject);
                checkBtnState();
            });
        } else if (id == R.id.re_mine) {
            PicChooseUtils.chooseOnePicWithCut(WechatVideoCallActivity.this, 1, 2, Pic -> {
                mStrMineImg = Pic[0];
                Picasso.get().load("file:" + mStrMineImg).into(mIvMine);
                checkBtnState();
            });
        } else if (id == R.id.btn_save) {
            String strTime = (mMin < 10 ? "0" + mMin : mMin) + ":" + (mSec < 10 ? "0" + mSec : mSec);
            WechatVideoCallDetailActivity.start(WechatVideoCallActivity.this, isToAnswer, strTime, mStrObjectImg, mStrMineImg, mReceiverContactBean);
        }
    }

    private void setState(boolean isToAnswer) {
        mTvToAnswer.setSelected(isToAnswer);
        mTvOnLine.setSelected(!isToAnswer);
        this.isToAnswer = isToAnswer;
        //通话画面
        mReTime.setVisibility(isToAnswer ? View.GONE : View.VISIBLE);
        mReObject.setVisibility(isToAnswer ? View.GONE : View.VISIBLE);
        //检查
        checkBtnState();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE_OTHER_RECEIVE || requestCode == REQUEST_CODE_CHOOSE_NEW_ADD_RECEIVE) {
            if (data == null)
                return;
            WechatContactBean result = (WechatContactBean) data.getSerializableExtra("result");
            if (result == null)
                return;
            //赋值
            mReceiverContactBean = result;
            //显示界面
            WechatContactBean.setupAvatarIntoImageView(mReceiverContactBean, mIvReceiver, 12);
            mTvReceiver.setText(TextUtils.isEmpty(mReceiverContactBean.getName()) ? "" : mReceiverContactBean.getName());
            //修改按钮状态
        }
        checkBtnState();
    }

    private void checkBtnState() {
        if (!isToAnswer) {
            if (mMin == 0 && mSec == 0) {
                mBtnSave.setEnabled(false);
                return;
            }
            if (TextUtils.isEmpty(mStrObjectImg)) {
                mBtnSave.setEnabled(false);
                return;
            }
            if (TextUtils.isEmpty(mStrMineImg)) {
                mBtnSave.setEnabled(false);
                return;
            }
        }
        if (mReceiverContactBean == null) {
            mBtnSave.setEnabled(false);
            return;
        }
        mBtnSave.setEnabled(true);
    }
}
